package handler

import (
	"2301a/goods/api/request"
	"2301a/goods/internal/dao"
	"2301a/goods/internal/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AddGoods(c *gin.Context) {
	var req request.AddGoodsReq
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息错误",
			"data": err.Error(),
		})
		return
	}
	var user model.User
	userId := c.GetUint("userId")
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}
	dao.Create(&model.Goods{
		UserId:    userId,
		GoodsName: req.GoodsName,
		Price:     req.Price,
		FileUrl:   req.FileUrl,
		Collect:   req.Collect,
		Describe:  req.Describe,
		Inventory: req.Inventory,
	})
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "商品添加成功",
		"data": true,
	})
}

func ModifyGoods(c *gin.Context) {
	var req request.ModifyGoodsReq
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息错误",
			"data": err.Error(),
		})
		return
	}
	var user model.User
	userId := c.GetUint("userId")
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}
	var goods model.Goods
	dao.GetOneById(req.Id, &goods)
	if goods.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "该权限修改失败",
			"data": nil,
		})
		return
	}
	dao.Update(&model.Goods{
		Id:        req.Id,
		UserId:    userId,
		GoodsName: req.GoodsName,
		Price:     req.Price,
		FileUrl:   req.FileUrl,
		Collect:   req.Collect,
		Describe:  req.Describe,
		Inventory: req.Inventory,
	})
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "商品修改成功",
		"data": goods,
	})
}

func ListGoods(c *gin.Context) {
	var req request.ListGoodsReq
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息错误",
			"data": err.Error(),
		})
		return
	}
	var user model.User
	userId := c.GetUint("userId")
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}
	var list []model.Goods
	dao.GetListByFields(&model.Goods{
		UserId:    userId,
		GoodsName: req.GoodsName,
	}, &list)
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "商品列表展示成功",
		"data": list,
	})
}

func PagingGoods(c *gin.Context) {
	var req request.PagingGoodsReq
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息错误",
			"data": err.Error(),
		})
		return
	}
	var user model.User
	userId := c.GetUint("userId")
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}
	pageDto := dao.PageDto{
		Page:     req.Page,
		PageSize: req.PageSize,
		OrderBy:  "created_at desc",
	}
	//paginate, _ := dao.Paginate(global.DB.Where(req.GoodsName), pageDto,dao.)
	//paginate, _ := dao.Paginate(global.DB.Where(req.GoodsName), pageDto, global.DB.Model(&goods))
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "商品分页成功",
		"data": pageDto,
	})
}

func CollectGoods(c *gin.Context) {
	var req request.CollectGoodsReq
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息错误",
			"data": err.Error(),
		})
		return
	}
	var user model.User
	userId := c.GetUint("userId")
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}
	dao.Create(&model.Goods{
		UserId:    userId,
		GoodsName: req.GoodsName,
		Price:     req.Price,
		FileUrl:   req.FileUrl,
		Collect:   req.Collect,
		Describe:  req.Describe,
		Inventory: req.Inventory,
	})
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "商品收藏成功",
		"data": true,
	})
}

func CancelGoods(c *gin.Context) {
	var req request.CancelGoodsReq
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息错误",
			"data": err.Error(),
		})
		return
	}
	var user model.User
	userId := c.GetUint("userId")
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}
	var goods model.Goods
	dao.GetOneById(req.Id, &goods)
	if goods.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "该权限修改失败",
			"data": nil,
		})
		return
	}
	dao.Update(&model.Goods{
		Id:      req.Id,
		UserId:  userId,
		Collect: req.Collect,
	})
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "取消收藏成功",
		"data": goods,
	})
}

func QueryGoods(c *gin.Context) {
	var req request.QueryGoodsReq
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数信息错误",
			"data": err.Error(),
		})
		return
	}
	var user model.User
	userId := c.GetUint("userId")
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}
	var list []model.Goods
	dao.GetListByFields(&model.Goods{
		UserId:    userId,
		GoodsName: req.GoodsName,
		Collect:   req.Collect,
	}, &list)
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "查询当前用户收藏商品",
		"data": list,
	})
}
